package com.horizon.mobile.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.horizon.mobile.adapter.NewFlowAdapter;
import com.horizon.mobile.manage.SharePreferences;
import com.horizon.mobile.model.Agent;
import com.horizon.mobile.model.Result;
import com.horizon.mobile.model.Todo;
import com.horizon.mobile.util.HttpRequest;

import java.util.ArrayList;
import java.util.List;

import zuo.biao.library.base.BaseHttpListFragment;
import zuo.biao.library.interfaces.AdapterCallBack;
import zuo.biao.library.interfaces.CacheCallBack;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.util.JSON;

public class NewFlowListFragment extends BaseHttpListFragment<Agent, NewFlowAdapter>
		implements CacheCallBack<Agent> {
	public static final String ARGUMENT_RANGE = "ARGUMENT_RANGE";
	public int page = 1;
	public List<Agent> listTodo = new ArrayList<Agent>();
	static NewFlowListFragment fragment;

	public static NewFlowListFragment createInstance() {
		if(fragment==null)
			fragment = new NewFlowListFragment();
		return  fragment;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		argument = getArguments();
		initCache(this);

		// 功能归类分区方法，必须调用<<<<<<<<<<
		initView();
		initData();
		initEvent();
		// 功能归类分区方法，必须调用>>>>>>>>>>

		lvBaseList.onRefresh();

		return view;
	}

	@Override
	public List parseArray(String json) {
		// TODO Auto-generated method stub
		return JSON.parseArray(json, Todo.class);
	}

	@Override
	public void getListAsync(int page) {
		this.page = page;
		HttpRequest.getNewFlowList(SharePreferences.getInstance(context)
				.getToken(),  page, 10,0, listener);
	}

	@Override
	public void initData() {// 必须调用
		super.initData();

	}

	@Override
	public Class<Agent> getCacheClass() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCacheGroup() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getCacheId(Agent data) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getCacheCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setList(final List<Agent> list) {
		listTodo = list;
		setList(new AdapterCallBack<NewFlowAdapter>() {

			@Override
			public void refreshAdapter() {
				adapter.refresh(list);

			}

			@Override
			public NewFlowAdapter createAdapter() {
				// TODO Auto-generated method stub
				return new NewFlowAdapter(context);
			}
		});

	}

	OnHttpResponseListener listener = new OnHttpResponseListener() {

		@Override
		public void onHttpResponse(int requestCode, String resultJson,
				Exception e) {
			boolean isNull = resultJson == null;
			if (resultJson != null) {
				Result result = JSON.parseObject(resultJson, Result.class);
				if (result.isSuccess()) {
					listTodo = JSON.parseArray(result.getData().toString(),
							Agent.class);
					onLoadSucceed(page, listTodo);
					// setList(list);
				} else {
					if (result != null)
						showShortToast(result.getMessage());
					else
						showShortToast("连接服务器失败！");
				}
			} else {
				if (e != null)
					showShortToast(e.getMessage());
				else
					showShortToast("连接服务器失败！");
			}
			onStopRefresh();
		}

	};

	@Override
	public void initEvent() {// 必须调用
		super.initEvent();

		lvBaseList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				toActivity(FlowDetailActivity.createIntent(context,
						"", listTodo.get(position-1)
								.getTitle(),listTodo.get(position-1).getFlowId(),""));
			}
		});
	}

}
